<?php
/**
 * User:于小涛
 * Date:2020/9/15
 * Time:14:07
 */
namespace app\api\controller\user;

use app\api\BaseController;
use app\api\model\rbac\Admin;
use app\api\validate\user\User as UserValidate;
use think\App;

class User extends BaseController{
    private $obj_user_validate=null;
    private $obj_admin_model=null;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_user_validate = new UserValidate();
        $this->obj_admin_model = new Admin();
    }
    public function login(){
        if(!$this->obj_user_validate->scene(__FUNCTION__)->check($this->request->param()))
        {
            trace("参数验证失败,".$this->obj_user_validate->getError(),'error');
            return output_data(array(),'paramserror',$this->obj_user_validate->getError());
        }
        $arr_admin_info=$this->obj_admin_model
            ->where('account',$this->request->param('account'))
            ->where('password',md5($this->request->param('password').$this->request->param('password')))
            ->select()
            ->toArray();
        if(empty($arr_admin_info)){
            return output_data(array(),'userloginerror',$this->obj_user_validate->getError());
        }else{
            $arr_admin_info=$arr_admin_info[0];
            if($arr_admin_info['is_lock']==2){
                return output_data(array(),'userloginlock',$this->obj_user_validate->getError());
            }
            //查询角色
            if(($arr_admin_info['type']==0)||($arr_admin_info['type']==1)){
                $str_role='all';
            }else{
                $str_sql="select * from rbac_admin_role where admin_id={$arr_admin_info['id']}";
                $arr_role_data=$this->obj_admin_model->query($str_sql);
                if(empty($arr_role_data)){
                    return output_data(array(),'userloginroleerror',$this->obj_user_validate->getError());
                }else{
                    $arr_role=[];
                    foreach($arr_role_data as $arr_item){
                        $arr_role[]=$arr_item['role_id'];
                    }
                    $str_role=implode(',',$arr_role);
                }
            }
            $arr_re=[
                'info'=>[
                    'name'=>$arr_admin_info['name'],
                    'token'=>$arr_admin_info['token'],
                ],
                'role_ids'=>$str_role,
            ];
            return output_data($arr_re);
        }
    }
}